<?php

namespace Modules\Common\General\Security\Api;


use Modules\Common\Models\AdminUser;
use Modules\Common\Models\Users;
use Illuminate\Support\Str;

class Gateway
{
    protected static $user;


    public static function setUser($user)
    {
        self::$user = $user;
    }

    public static function user()
    {
        return self::$user;
    }

    public static function AdminUser(): AdminUser
    {
        return self::user();
    }

    public static function generateTokenStringAndHash()
    {
        $tokenString = self::generateTokenString();
        $tokenHash = self::hash($tokenString);
        return [$tokenString, $tokenHash];
    }

    public static function hash($string)
    {
        return hash_hmac('sha256', $string, config('app.key'));
    }

    private static function generateTokenString()
    {
        $microTime = str_replace('.', '', microtime(true));
        return sprintf('$%s$%s', $microTime, Str::random(40));
    }
}
